#ifndef GAMEWIDGET_H
#define GAMEWIDGET_H

#include <QTimer>
#include <QWidget>
#include <QPainter>
#include <QList>
#include <QPixmap>

class GameWidget : public QWidget {
    Q_OBJECT
public:
    explicit GameWidget(QWidget *parent = nullptr);
    void initGame();

protected:
    void paintEvent(QPaintEvent *) override;
    void keyPressEvent(QKeyEvent *event) override;

private slots:
    void gameUpdate();

private:
    enum Direction { Up, Down, Left, Right };

    QTimer *timer;
    QList<QPoint> snake;
    QPoint food;
    Direction dir;
    bool speedUp;
    int cellSize;
    QColor foodColor;

    QPixmap m_background{":/new/prefix1/image/b2.jpg"}; // 背景图片
    void generateFood();
    bool checkCollision();
    void gameOver();
};

#endif // GAMEWIDGET_H
